﻿using AutoMapper;
using MediatR;
using Traceability.Doamin;
using Traceability.ErrorCode;
using Traceability.Infratruvture.Interfares;
using Traceability.Write.Api.Application.Command;

namespace Traceability.Write.Api.Application.CommandHander
{
    public class UpdateImmunityHander : IRequestHandler<UpdateImmunityCommand, ApiResult<int>>
    {

        private readonly IMapper mapper;
        private readonly IBaseRequsitory<Immunity> baseRequsitory;

        public UpdateImmunityHander(IMapper mapper, IBaseRequsitory<Immunity> baseRequsitory)
        {
            this.mapper = mapper;
            this.baseRequsitory = baseRequsitory;
        }

        /// <summary>
        /// 修改免疫信息操作
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(UpdateImmunityCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var entity = mapper.Map<Immunity>(request);
            await baseRequsitory.UpdateAsync(entity);

            result.Code = ApiEnum.修改成功;
            result.Msg = ApiEnum.修改成功.ToString();

            return await Task.FromResult(result);
        }
    }
}
